package pl.edu.agh.gronoMining.owlToGraphConverter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import pl.edu.agh.gronoMining.owlToGraphConverter.graph.Graph;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.GraphParser;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.implementation.GronoOwlParser;



public class Main {

	private static final String LOGGER_PROP_FILE = "resources/logger.properties.xml";
	
	private static Logger logger = Logger.getLogger(Main.class);
	
	public static void main(String[] args) {
		
		DOMConfigurator.configure(LOGGER_PROP_FILE);
		
		Settings settings = new Settings();
		if (! settings.parseSettings(args)) {
			System.out.println("wrong arguments");
			System.out.println(settings.getArgumentsInfo());
		}
		logger.info("----------------");
		logger.info("input file   -> "+settings.getProperty(Settings.INPUT_FILE_PROP_NAME));
		logger.info("gSpan input  -> "+settings.getProperty(Settings.GSPAN_INPUT_FILE_PROP_NAME));
		logger.info("gSpan output -> "+settings.getProperty(Settings.GSPAN_OUTPUT_FILE_PROP_NAME));
		logger.info("output file  -> "+settings.getProperty(Settings.OUTPUT_FILE_PROP_NAME));
		logger.info("min Freq     -> "+settings.getProperty(Settings.MINIMUM_FREQUENCIES));
		logger.info("----------------");
		
		GraphParser parser = new GronoOwlParser();
		
		
		try {
			File inputFile = new File(settings.getProperty(Settings.INPUT_FILE_PROP_NAME));
			InputStream inputStream = new FileInputStream(inputFile);
			
			File gSpanOInputFile = new File(settings.getProperty(Settings.GSPAN_INPUT_FILE_PROP_NAME));
			gSpanOInputFile.createNewFile();
			OutputStream gSpanOutputStream = new FileOutputStream(gSpanOInputFile);
			
			Graph[] graphs = parser.parseFromOwl(inputStream);
			
			//GraphVisualiser inputGraphVisualiser = new GraphVisualiser("input graph");
			//inputGraphVisualiser.show(graphs); 
			
			parser.serializeToSimple(graphs, gSpanOutputStream);
			
			inputStream.close();
			gSpanOutputStream.close();
			
			logger.info("file to gSpan created");
			
			/*
			logger.info("starting gSpan");
			
			Set<String> minerArgsSet = new HashSet<String>();
			minerArgsSet.add("-minimumFrequencies="+settings.getProperty(Settings.MINIMUM_FREQUENCIES));
			minerArgsSet.add("-graphFile="+settings.getProperty(Settings.GSPAN_INPUT_FILE_PROP_NAME));
			minerArgsSet.add("-outputFile="+settings.getProperty(Settings.GSPAN_OUTPUT_FILE_PROP_NAME));
			minerArgsSet.add("-parserClass=de.parmol.parsers.LineGraphParser");
			//minerArgsSet.add("-checkFragmentCounts=true");
			
			String[] minerArgs = new String[minerArgsSet.size()];
			int i = 0;
			for (String s : minerArgsSet)
				minerArgs[i++] = s;
			try {
				logger.log("args -> "+minerArgs);
				Miner.main(minerArgs);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			logger.log("end gSpan");
			
			logger.log("parsing results");
			File gSpanOutputFile = new File(settings.getProperty(Settings.GSPAN_OUTPUT_FILE_PROP_NAME));
			InputStream resultsInputStream = new FileInputStream(gSpanOutputFile);
			
			Graph[] resultGraphs = parser.parseFromLine(resultsInputStream);
			logger.log("results parsed");
			
			logger.log("showing results");
			GraphVisualiser resultGraphVisualiser = new GraphVisualiser("result graphs");
			resultGraphVisualiser.show(resultGraphs);

			logger.log("saving results");
			File outputFile = new File(settings.getProperty(Settings.OUTPUT_FILE_PROP_NAME));
			outputFile.createNewFile();
			OutputStream outOutputStream = new FileOutputStream(outputFile);
			
			parser.serializeToExtended(resultGraphs, outOutputStream);
			logger.log("resultss saved");*/
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		logger.info("end");
	}	
}
